// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  var res = await cloud.database().collection('discuss-post').aggregate()
    .match({
      isShow: true,
      _openid: wxContext.OPENID
    })
    .sort({
      _updateTime: -1
    })
    .project({
      _id: 1,
      _openid: 1,
      _createTime: 1,
      comments: 1,
      contentPreview: 1,
      likes: 1
    })
    .lookup({
      from: 'user',
      localField: '_openid',
      foreignField: '_id',
      as: 'poster',
    })
    .skip(event.len)
    .limit(20)
    .end()
  return res
}